{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b29d23c8",
   "metadata": {},
   "source": [
    "# Basic Browser tool usage with Amazon Nova Act SDK\n",
    "\n",
    "## Overview\n",
    "\n",
    "In this tutorial we will learn how to use Nova Act SDK to use with Amazon Bedrock Agentcore Browser tool. We will provide examples of using the browser tool headless and view the browser live.\n",
    "\n",
    "\n",
    "### Tutorial Details\n",
    "\n",
    "\n",
    "| Information         | Details                                                                          |\n",
    "|:--------------------|:---------------------------------------------------------------------------------|\n",
    "| Tutorial type       | Conversational                                                                   |\n",
    "| Agent type          | Single                                                                           |\n",
    "| Agentic Framework   | Nova Act                                                                         |\n",
    "| LLM model           | Amazon Nova Act model                                                            |\n",
    "| Tutorial components | Using NovaAct to interact with browser tool in a headless way                    |\n",
    "| Tutorial vertical   | vertical                                                                         |\n",
    "| Example complexity  | Easy                                                                             |\n",
    "| SDK used            | Amazon BedrockAgentCore Python SDK, Nova Act                                     |\n",
    "\n",
    "### Tutorial Architecture\n",
    "\n",
    "In this tutorial we will describe how to use Nova Act with browser tool.  \n",
    "\n",
    "In our example we will send natural language instructions to the Nova Act agent to perform tasks on the Bedrock Agentcore browser in a headless way.\n",
    "\n",
    "<div style=\"text-align:left\">\n",
    "    <img src=\"images/architecture_runtime.png\" width=\"50%\"/>\n",
    "</div>\n",
    "\n",
    "### Tutorial Key Features\n",
    "\n",
    "* Using browser tool in a headless way\n",
    "* Using Nova Act with browser tool"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41b469ca",
   "metadata": {},
   "source": [
    "## Prerequisites\n",
    "\n",
    "To execute this tutorial you will need:\n",
    "* Python 3.10+\n",
    "* AWS credentials\n",
    "* Amazon Bedrock AgentCore SDK\n",
    "* Nova Act SDK and API key "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33245169",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "513a5a71",
   "metadata": {},
   "source": [
    "## Using NovaAct with Bedrock Agentcore Browser tool\n",
    "We will create a python script that can be run locally. In the script, Nova Act will use the browser session's CDP endpoint to connect to it and perform playwright actuations. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff93fb3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%writefile basic_browser_with_nova_act.py\n",
    "\"\"\"Browser automation script using Amazon Bedrock AgentCore and Nova Act.\n",
    "\n",
    "This script demonstrates AI-powered web automation by:\n",
    "- Initializing a browser session through Amazon Bedrock AgentCore\n",
    "- Connecting to Nova Act for natural language web interactions\n",
    "- Performing automated searches and data extraction using browser\n",
    "\"\"\"\n",
    "\n",
    "from bedrock_agentcore.tools.browser_client import browser_session\n",
    "from nova_act import NovaAct\n",
    "from rich.console import Console\n",
    "import argparse\n",
    "import json\n",
    "\n",
    "console = Console()\n",
    "\n",
    "from boto3.session import Session\n",
    "\n",
    "boto_session = Session()\n",
    "region = boto_session.region_name\n",
    "print(\"using region\", region)\n",
    "\n",
    "def browser_with_nova_act(prompt, starting_page, nova_act_key, region=\"us-west-2\"):\n",
    "    with browser_session(region) as client:\n",
    "        ws_url, headers = client.generate_ws_headers()\n",
    "        try:\n",
    "            with NovaAct(\n",
    "                cdp_endpoint_url=ws_url,\n",
    "                cdp_headers=headers,\n",
    "                preview={\"playwright_actuation\": True},\n",
    "                nova_act_api_key=nova_act_key,\n",
    "                starting_page=starting_page,\n",
    "            ) as nova_act:\n",
    "                result = nova_act.act(prompt)\n",
    "        except Exception as e:\n",
    "            console.print(f\"NovaAct error: {e}\")\n",
    "        return result\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    parser = argparse.ArgumentParser()\n",
    "    parser.add_argument(\"--prompt\", required=True, help=\"Browser Search instruction\")\n",
    "    parser.add_argument(\"--starting-page\", required=True, help=\"Starting URL\")\n",
    "    parser.add_argument(\"--nova-act-key\", required=True, help=\"Nova Act API key\")\n",
    "    parser.add_argument(\"--region\", default=\"us-west-2\", help=\"AWS region\")\n",
    "    args = parser.parse_args()\n",
    "\n",
    "    result = browser_with_nova_act(\n",
    "        args.prompt, args.starting_page, args.nova_act_key, args.region\n",
    "    )\n",
    "    console.print(f\"\\n[cyan] Response[/cyan] {result.response}\")\n",
    "    console.print(f\"\\n[bold green]Nova Act Result:[/bold green] {result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "242ce323",
   "metadata": {},
   "source": [
    "#### Running the script\n",
    "Paste your Nova Act API key below before running the script. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a1a93105",
   "metadata": {},
   "outputs": [],
   "source": [
    "NOVA_ACT_KEY= '' ### Paste your Nova Act Key here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "405a6753",
   "metadata": {},
   "outputs": [],
   "source": [
    "!python basic_browser_with_nova_act.py --prompt \"Search for macbooks and extract the details of the first one\" --starting-page \"https://www.amazon.com/\" --nova-act-key {NOVA_ACT_KEY}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84a53dff",
   "metadata": {},
   "outputs": [],
   "source": [
    "!python basic_browser_with_nova_act.py --prompt \"Extract and return today's market gainer and loser's details\" --starting-page \"https://finance.yahoo.com/\" --nova-act-key {NOVA_ACT_KEY}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1bc93bb",
   "metadata": {},
   "source": [
    "## What happened behind the scenes?\n",
    "\n",
    "* When you used `browser_session` to instantiate a browser client, it created a Browser client and started a session\n",
    "* Then you configured Nova Act to point to that browser session using the `cdp_endpoint_url` and `cdp_headers` \n",
    "* Nova ACT SDK now took your natural language instructions and created Playwright actuations on the browser. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2945f9ba",
   "metadata": {},
   "source": [
    "# Congratulations!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "agentcore",
   "language": "python",
   "name": "agentcore"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
